package wildlife;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.File;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport implements
		ServletRequestAware {
	private File userImage;
	private String userImageContentType;
	private String userImageFileName;
	Map session;

	private HttpServletRequest servletRequest;

	public String execute() {
		try {
			session=ActionContext.getContext().getSession();
			HttpServletRequest request=ServletActionContext.getRequest();
			HttpSession session=request.getSession();
			String filePath = "../images/";
			System.out.println("Server path:" + filePath);
			File fileToCreate = new File(filePath,session.getAttribute("username").toString()+".jpg");

			FileUtils.copyFile(this.userImage, fileToCreate);
			SaveImageToDatabase d=new SaveImageToDatabase();
			d.insert(userImage);
		} catch (Exception e) {
			e.printStackTrace();
			addActionError("NOT AN IMAGE");

			return INPUT;
		}
		
		
		return SUCCESS;
	}

	public File getUserImage() {
		return userImage;
	}

	public void setUserImage(File userImage) {
		this.userImage = userImage;
	}

	public String getUserImageContentType() {
		return userImageContentType;
	}

	public void setUserImageContentType(String userImageContentType) {
		this.userImageContentType = userImageContentType;
	}

	public String getUserImageFileName() {
		return userImageFileName;
	}

	public void setUserImageFileName(String userImageFileName) {
		this.userImageFileName = userImageFileName;
	}

	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;

	}
}